<?php
//*****************************************************************//
// Pixie: The Small, Simple, Site Maker.                           //
// ----------------------------------------------------------------//
// Licence: GNU General Public License v3                          //
// Title: Your Module Name                                         //
//*****************************************************************//

// The module is loaded into Pixie in many different instances, the variable
// $do is used to run the module in different ways.
switch ($do) {

    // General information:
    // The general information is used to show infromation about the module within Pixie. 
    // Simply enter details of your module here:
    case "info":
       // The name of your module
       $m_name = "Photo Gallery";
       // A description of your module
       $m_description = "Create a photo gallery.";
       // Who is the module author?
       $m_author = "Temple Team - Swinburne";
       // What is the URL of your homepage
       $m_url = "http://cit3.ldl.swin.edu.au/~temple/";
       // What version is this?
       $m_version = "1";
       // Can be set to module or plugin.
       $m_type = "module";
       // Is this a module that needs publishing to?
       $m_publish = "yes";
       
    break;

    // Install
    // This section contains the SQL needed to create your modules tables
    case "install":
        // Create any required tables
        $execute = "CREATE TABLE IF NOT EXISTS `pixie_module_galleries` (`galleries_id` int(4) NOT NULL auto_increment,`title` varchar(150) collate utf8_unicode_ci NOT NULL default '',`date` timestamp NOT NULL default CURRENT_TIMESTAMP,`summary` varchar(100) collate utf8_unicode_ci NOT NULL default '',`description` varchar(400) collate utf8_unicode_ci NOT NULL default '',`sourcefolder` varchar(100) collate utf8_unicode_ci NOT NULL UNIQUE default '',PRIMARY KEY  (`galleries_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;";
    break;

    // The administration of the module (add, edit, delete)
    // This is where Pixie really saves you time, these few lines of code will create the entire admin interface
    case "admin":
       // The name of your module
       $module_name= "Galleries";
       // The name of the table                                                             
       $table_name = 'pixie_module_galleries';
       // The field to order by in table view                                                       
       $order_by = 'date';
       // Ascending (asc) or decending (desc)                                                           
       $asc_desc = "desc";
       // Fields you want to exclude in your table view                                                             
       $view_exclude = array('galleries_id','date');
       // Fields you do not want people to be able to edit          
       $edit_exclude = array('galleries_id','date');
       // The number of items per page in the table view
       $items_per_page = "15";
       // Does this module support tags (yes or no)                                                     
       $tags = "no";
       
       admin_module($module_name,$table_name,$order_by,$asc_desc,$view_exclude,$edit_exclude,$items_per_page, $tags);

    break;
    
    // The three sections below are all for the module output, a module is loaded at three different stages of a page build.
    // If you need to declare functions you must do so in a seperate file. For this example links module I would create a
    // file called links_functions.php and place it in the modules folder. Pixie will include this once before running the Pre
    // section below.
    
    // Pre
    // Any code to be run before HTML output, any redirects or header changes must occur here
    case "pre":
    
        // Two important variables to mention here:
        // $ptitle - To overwrite the current page title
        //$ptitle = "Photo Galleries";
        // $pinfo - To overwrite the strapline and meta description.
        //$pinfo = "This is my links page and its really great.";
        
    break;
    
    // Head
    // This will output code into the end of the head section of the HTML, this allows you to load in external CSS, JavaScript etc
    
    case "head":
         //For light box:
     print "<script type=\"text/javascript\" src=\"lightbox/js/prototype.js\"></script>";
   print "<script type=\"text/javascript\" src=\"lightbox/js/scriptaculous.js?load=effects,builder\"></script>";
   print "<script type=\"text/javascript\" src=\"lightbox/js/lightbox.js\"></script>";
   print "<link rel=\"stylesheet\" href=\"lightbox/css/lightbox.css\" type=\"text/css\" media=\"screen\" />";
    break;

    // Show Module
    // This is where your module will output into the content div on the page
    default:
            
        if(!isset($_GET['id']))
        {
            //List all available galleries, in reverse date order
            
            //Get all the available IDs
            $galleryIDs = array();
            $galleryIDs = getGalleryListOfIDs();
            
            $galleryInfo = array();
            //Get and display the details of each available gallery:
            //
            for ($i=0; $i < sizeof($galleryIDs); $i++)
            {
                //Get the title, summary and date of the gallery
                $galleryInfo = getGalleryInfo($galleryIDs[$i]);
                //Use the date format function to make the date look nice
                $date = formatDate($galleryInfo[3]);
                print "<h4>$galleryInfo[0]</h4>"; //Print the gallery title
                print "<p>$galleryInfo[1]<br />";
                print $date."&nbsp;&nbsp - &nbsp;&nbsp;<a href=\"?s=$s&id=$galleryIDs[$i]\">View</a></p>"; //Print a summary of the gallery, and the creation date
            }
        }
        else
        {
            //Here's where all the magic happens
                        
            //Get the gallery ID number
            $galleryID = $_GET['id'];
            
            //Get all the gallery details and store them in an array:
            //Title, Summary, Description, Date, Source Folder
            $galleryInfo = array();
            $galleryInfo = getGalleryInfo($galleryID);
            $date = formatDate($galleryInfo[3]);
            
            //Display the title
            print "<h4>$galleryInfo[0]</h4>";
            //Display the summary, followed by the creation date
            print "<p>$galleryInfo[2]<br />";
            print $date."</p>";
            
            //Codes for displaying thumbnails on page - written by Cladine & Karen on 28 Oct 08:

            //Get the source folder from the database
            //$dirSourceFolder = "files/galleries/".getSourceFolder($galleryID);
            $dirSourceFolder = "files/galleries/".$galleryInfo[4];
            //Create variables for the thumbs and the images folders
            $dirThumbs = $dirSourceFolder."/thumbs";
            $dirImages = $dirSourceFolder."/images";
            
            //create thumbnails
            if (!file_exists($dirThumbs))
            {
                mkdir($dirThumbs);
                createThumbs ($dirImages, $dirThumbs, 70);
            }   
            //Create an array to store all the filenames of files that 
            //are in the thumbs directory
            $files = array();
            $index = 0;
        
           // Open a known directory, and proceed to read its contents
            if (is_dir($dirThumbs) && is_dir($dirImages) && is_dir($dirSourceFolder)) 
            {
                if ($dh = opendir($dirThumbs)) 
                {
                    while (($file = readdir($dh)) !== false) {
                       $files[$index] = $file;
                       $index ++;
                }
                closedir($dh);
            }
            
            print "<div id='gallery'><ul id='gallerylist'>";
            foreach ($files as $file)
            {
                if (isImage($file))
                {
                    print "<li><a rel=\"lightbox\" href=\"$dirImages/$file\" >
                    <img src =\"$dirThumbs/$file\" /></a></li>";
                }
            }
            print "</ul></div>";
        }           
        
    break;
    }
}
?>
